/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vendingmachine;

import java.util.Scanner;

/**
 *
 * @author mghufronm
 */
public class VendingMachine {
    
    private ObjectReceiver objReceiver;
    private ProductStorage prodStorage;
    private CoinStorage coinStorage;
    private ProductSelector prodSelector;
    private EjectButton ejectButton;
    private ProductDispenser prodDisp;
    private ObjectDispenser objDisp;

    public VendingMachine() throws FileExp {
        objReceiver = new ObjectReceiver();
        prodStorage = new ProductStorage();
        coinStorage = new CoinStorage(50);
        prodSelector = new ProductSelector();
        ejectButton = new EjectButton();
        prodDisp = new ProductDispenser();
        objDisp = new ObjectDispenser();
        
        int i = 0;
        while (i<25){
            coinStorage.getNickelTray().add(CoinValidator.getNickel());
            coinStorage.getDimeTray().add(CoinValidator.getDime());
            coinStorage.getQuarterTray().add(CoinValidator.getQuarter());
            i++;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileExp {
        // TODO code application logic here
        VendingMachine vm = new VendingMachine();
        boolean stop = false;
        while(stop == false){
            System.out.println("\nChoose an action in to vending machine (1, 2, 3, 4):");
            System.out.println("1. Insert an object");
            System.out.println("2. Select a product");
            System.out.println("3. Press force eject button");
            System.out.println("4. Quit");
            System.out.print(">");
            Scanner scan = new Scanner(System.in);
            try{
                int selectedAction = scan.nextInt();
                switch(selectedAction){
                    case 1: {
                        vm.objReceiver.readInput();
                        break;
                    }
                    case 2: {
                        vm.prodSelector.readSelectionId(vm.objReceiver, vm.prodStorage, vm.coinStorage, vm.prodDisp, vm.objDisp);
                        break;
                    }
                    case 3: {
                        vm.ejectButton.trigger();
                        vm.objReceiver.resetValid();
                        break;
                    }
                    case 4: {
                        stop = true;
                        break;
                    }
                    case 99:{
                        vm.coinStorage.manageCoinStorage();
                        vm.prodStorage.refillAllProduct();
                        break;
                    }
                    default: {
                        System.out.println("Aksi diluar pilihan, silahkan ulangi..");
                        break;
                    }
                }
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
    }
}
